<?php

class Login extends Login_model {

    function __construct() {
        parent::__construct();
    }
    
    function validateChangePassword($new_password, $confirm_password) {
        if (trim($new_password) == "")
        {
            MessageHandler::add (_LB_INVALID_PASSWORD, MSG_ERROR, LOG_ONLY);
        }
        elseif(strlen($new_password) > MAX_LENGTH_PASSWORD)
        {
            MessageHandler::add (_MSG_PASSWORD_IS_TOO_LONG, MSG_ERROR, LOG_ONLY);
        }
        elseif(strlen($new_password) < MIN_LENGTH_PASSWORD)
        {
            MessageHandler::add (_MSG_PASSWORD_IS_TOO_SHORT, MSG_ERROR, LOG_ONLY);
        }
        elseif (trim($confirm_password) == "") {
            MessageHandler::add (_LB_INVALID_CONFIRM_PASSWORD, MSG_ERROR, LOG_ONLY);
        } elseif ($new_password != $confirm_password)
        {
            MessageHandler::add (_LB_PASSWORD_DOESNT_MATCH, MSG_ERROR, LOG_ONLY);
        }
        return MessageHandler::countError() > 0 ? false : true;
    }
    
	/**
	 * Check if email of the user exist
	 *
	 * @param string $email
	 * @return int $id is the Login ID
	 */
	function userExistByemail($email) {

		$t = new Login();
		$t->addWhere("email = '$email'");
		$t->addWhere("disabled=0");;
		$t->find();
        if($t->fetchFirst()) {
            return $t->id;
        }
        return 0;
	}
    
    /**
	 * Check if email of the user exist
	 *
	 * @param string $email
	 * @param string $loginID
	 * @return int $id is the Login ID
	 */
	function emailExistByLoginID($email, $loginID) {
		$t = new Login();
		$t->addWhere("email = '$email'");
		if (trim($loginID) != '' && $loginID != 0)
		{
			$t->addWhere("id <> " . $loginID);
		}
		$t->addWhere("disabled=0");
		$t->find();
		if($t->fetchFirst()) {
            return $t->id;
        }
        return 0;
	}
    /**
	 * Insert/Update the Login
	 *
	 * @return data_object updated
	 */
	function store() {
		if ($this->id==0)
			return $this->insert();
		else return $this->update();
	}
    
    function updateLogin() {
		$this->date_last_login = date("Y-m-d H:i:s");
		$this->update();
	}
    
    function isExistLoginID()
	{
		$login = new Login();
		$login->addWhere('id = ' . $_SESSION['userID']);
		$login->addWhere('disabled = 0');
        $login->find();
		if ($login->countRows() > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
    
    function increaseLoginAttempts($login)
	{
		if ($login)
		{
			$obj_login = new Login();
			$obj_login->addWhere("email = '$login'");
			$obj_login->find();

			if ($obj_login->fetchNext())
			{
				$obj_login->login_attempts += 1;

				if ($obj_login->login_attempts >= ALLOWED_LOGIN_ATTEMPTS)
				{
					$obj_login->deactived = 1;
				}

				$obj_login->update();
			}
		}
	}
    
    function resetLoginAttempts($login)
	{
		if ($login)
		{
			$obj_login = new Login();
			$obj_login->addWhere("email = '$login'");
			$obj_login->find();
			if ($obj_login->fetchNext())
			{
				if (!$obj_login->deactived)
				{
					$obj_login->login_attempts = 0;
					$obj_login->update();
				}
			}
		}
	}
    
    public static function isPassTransformRule($new_login)
	{
		$bln_pass = true;

		// if transform-to account is SUPPORT or REGIE
		if ($new_login->is_controller)
		{
			if (($_SESSION["is_controller"] == ADMIN_SUPPORT && $new_login->is_controller == ADMIN_SUPPORT))
			{
				$bln_pass = false;
			}
		}
		return $bln_pass;
	}
    
    function getList($filter = array()) {
        $user = new Login();
        if(isset($filter['name']) && $filter['name']) {
            $user->addWhere("login.last_name LIKE '%".$filter['name']."%' or login.first_name LIKE '%".$filter['name']."%'");
        }
        if(isset($filter['type']) && $filter['type']) {
            $user->addWhere("login.is_controller = '".$filter['type']."'");
        }
        if($_SESSION["userID"] != ADMIN_SUPPORT) {
            //$user->addWhere("login.is_controller = ".$this->session->userdata("userID"));
        }
        $user->addSelect();
        $user->addSelect("login.*");
        $user->orderBy("login.last_name");
        $user->find();
        
        return $user;            
    }

	function validateInput($confirm_password) {
		
        if (trim($this->first_name) == "") {
			MessageHandler::add (_LB_FIRST_NAME_IS_EMPTY, MSG_ERROR, LOG_ONLY);
		}
		else if (strlen($this->first_name) > MAX_LENGTH_NAME) {
			MessageHandler::add (_LB_FIRST_NAME_IS_TOO_LONG, MSG_ERROR, LOG_ONLY);
		}
        if (trim($this->last_name) == "") {
			MessageHandler::add (_LB_NAME_IS_EMPTY, MSG_ERROR, LOG_ONLY);
		}
		if (!strlen($this->last_name) > MAX_LENGTH_NAME) {
			MessageHandler::add (_LB_NAME_IS_TOO_LONG, MSG_ERROR, LOG_ONLY);
		}
        
		if (!isset($this->email) || trim($this->email) == "") {
			MessageHandler::add (_LB_EMAIL_IS_EMPTY, MSG_ERROR, LOG_ONLY);
		}
		elseif (!isValidEmail($this->email))
		{
			MessageHandler::add (_LB_INVALID_EMAIL, MSG_ERROR, LOG_ONLY);
		}
		else if (strlen($this->email) > MAX_LENGTH_NAME)
		{
			MessageHandler::add (_LB_EMAIL_IS_TOO_LONG, MSG_ERROR, LOG_ONLY);
		}
		else if (Login::emailExistByLoginID($this->email, $this->id))
		{
			MessageHandler::add (_MSG_EMAIL_EXISTS, MSG_ERROR, LOG_ONLY);
		}

		// When add new
		if (!$this->id)
		{
			$this->validateChangePassword($this->pass, $confirm_password);
		}

        if (trim($this->is_controller) == "") {
			MessageHandler::add (_LB_NAME_IS_EMPTY, MSG_ERROR, LOG_ONLY);
		}
        
		return MessageHandler::countError() > 0 ? false : true;
	}
    
    function isdeactived($login)
	{
		$bln_ok = false;

		if ($login)
		{
			$obj_login = new Login();
			$obj_login->addWhere("email = '$login'");
			$obj_login->find();
			if ($obj_login->fetchNext())
			{
				if ($obj_login->deactived)
				{
					$bln_ok = true;
				}
			}
		}
		return $bln_ok;
	}
    
    function isAllowedAccessProject() {

		if ( $this->is_controller == ADMIN_SUPPORT && !ALLOW_ADMIN_SUPPORT ) {
            return false;
        } else if ($this->is_controller == ADMIN && !ALLOW_ADMIN) {
            return false;
        } else if ($this->is_controller == MANAGER && !ALLOW_MANAGER) {
            return false;
        }
		return TRUE;
	}
    
    function isDeactivated($login)
	{
		$bln_ok = false;

		if ($login)
		{
			$obj_login = new Login();
			$obj_login->addWhere("email = '$login'");
            $obj_login->find();
			if ($obj_login->fetchNext())
			{
            	if ($obj_login->deactived)
				{
					$bln_ok = true;
				}
			}
		}

		return $bln_ok;
	}
}
